package com.jvup.common.utils;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PointF;
import android.net.Uri;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

//import com.jvup.gameshow.BuildConfig;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class AppHelper {

	public static String IMEI = null;
	public static int appVersionCode = 0;
	public static String appVersionName = null;

	public static void simpleSetting(Activity activity) {
		Intent intent = new Intent();
		try{
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
			intent.setData(uri);
			activity.startActivity(intent);
		} catch (Exception e) {//抛出异常就直接打开设置页面
			intent = new Intent(Settings.ACTION_SETTINGS);
			activity.startActivity(intent);
		}
	}

	public static float distance(PointF p1, PointF p2) {
		float dx = p1.x - p2.x;
		float dy = p1.y - p2.y;
		return (float) Math.sqrt(dx * dx + dy * dy);
	}

//	public static Uri uriFromFile(Context context, File file) {
//		return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
//	}


	/**
	 *
	 * @param context
	 * @return IMEI
	 */
	public static String getDeviceIMEI(Context context) {
		if (IMEI == null) {
			TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

			if(TelephonyMgr == null) return null;
			if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
				return null;
			}
			IMEI = TelephonyMgr.getDeviceId();
		}
		return IMEI;
	}

	/**
	 * @param context
	 * @return versionCode 返回当前应用的版本编号
	 */
	public static int getVersionCode(Context context) {
		if(appVersionCode == 0){
			PackageManager pm = context.getPackageManager();//拿到包的管理器
			try {
				//封装了所有的功能清单中的数据
				PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
				appVersionCode = info.versionCode;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}
		return appVersionCode;
	}
	
	/**
	 * @param context
	 * @return versionCode 返回当前应用的版本编号
	 */
	public static String getVersionName(Context context) {
		if(appVersionName == null){
			PackageManager pm = context.getPackageManager();//拿到包的管理器
			try {
				//封装了所有的功能清单中的数据
				PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
				appVersionName = info.versionName;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
		}
		return appVersionName;
	}
	
	/**
	 * 获取剩余的内存
	 * @param context
	 * @return
	 */
	public static  long getAvailablyMemory(Activity context){
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		MemoryInfo mi = new MemoryInfo();
		am.getMemoryInfo(mi);
		
		return mi.availMem/(1024*1024);
	}
	
	/**
	 * 获得Application里面的的meta-data的值
	 * @param context
	 * @return
	 */
	public static String getMetaDataInApplication(Context context, String meta_data_Name) {
		// TODO Auto-generated method stub
		String ret = null;
		try {
			ApplicationInfo appInfo = context.getPackageManager()
					.getApplicationInfo(context.getPackageName(),
							PackageManager.GET_META_DATA);
			if((appInfo != null) && (appInfo.metaData != null)){
				ret = appInfo.metaData.get(meta_data_Name).toString();
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	public static String getChannel(Context context) {
        String sourceDir = context.getApplicationInfo().sourceDir;
        String ret = "";
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.startsWith("META-INF/channel")) {
                    ret = entryName;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        String[] split = ret.split("_");
        if (split != null && split.length >= 2) {
            return ret.substring(split[0].length() + 1);
        } else {
            return "0";
        }
    }

//
//	public static void launchWechat(Context context){
//		if(context == null){
//			return;
//		}
//		Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
//
//		try {
//			if (intent == null){
//				ToastHelper.showToast("没有安装微信");
//			}
//			context.startActivity(intent);
//		} catch (android.content.ActivityNotFoundException e) {
//			ToastHelper.showToast("没有安装微信");
//			e.printStackTrace();
//		}
//	}
//
//	public static void launchQQ(Context context, String qqNumber){
//		Uri uri = Uri.parse(Constants.URL_CONTACT+qqNumber);
//		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//		try {
//			context.startActivity(intent);
//		} catch(android.content.ActivityNotFoundException e){
//			if(context != null){
//				ToastHelper.showToast("请先安装QQ");
//			}
//		}
//
//	}

}
